from PIL import Image

"""
将像素转换为字符，写入txt中，最简单的玩法，不写入Main.py，仅做学习
"""

path = "1.jpg"

img = Image.open(path).convert("L")
width, height = img.size

width = int(width / 10)
height = int(height / 10)

out = img.resize((width, height))

temp = "@W#$OEXC[(/?=^~_.` "
texts = ""
for row in range(height):
    for col in range(width):
        gray = out.getpixel((col, row))  # 元组输入
        texts += temp[int(gray * 18 / 255)]
    texts += "\n"

with open(path.replace('jpg', 'txt'), 'w') as f:
    f.write(texts)
